package com.chen10.cattle.service.impl;

import com.chen10.cattle.mapper.CattleMapper;
import com.chen10.cattle.model.page.Pager;
import com.chen10.cattle.model.pojo.Cattle;
import com.chen10.cattle.model.pojo.CattleTemperature;
import com.chen10.cattle.model.pojo.DeathCattle;
import com.chen10.cattle.model.vo.*;
import com.chen10.cattle.service.CattleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CattleServiceImpl implements CattleService {
    @Resource
    private CattleMapper cattleMapper;
    @Override
    public List<CattleListVO> getList() {

        return cattleMapper.selectAll();
    }


    @Override
    public Pager<TemperatureHistoryVO> getTemperatureHistory(Long cattleId, Integer page, Integer pageSize) {
        int offset = (page - 1) * pageSize;
        List<TemperatureHistoryVO> list = cattleMapper.selectTemperatureHistory(cattleId, offset, pageSize);
        int total = cattleMapper.countTemperatureHistory(cattleId);

        Pager<TemperatureHistoryVO> pager = new Pager<>();
        pager.setPage(page);
        pager.setSize(pageSize);
        pager.setData(list);
        pager.setTotal(total);

        return pager;
    }


    @Override
    public Pager<WeightHistoryVO> getWeightHistory(Long cattleId, Integer page, Integer pageSize) {
        int offset = (page - 1) * pageSize;
        List<WeightHistoryVO> list = cattleMapper.selectWeightHistory(cattleId, offset, pageSize);
        int total = cattleMapper.countWeightHistory(cattleId);

        Pager<WeightHistoryVO> pager = new Pager<>();
        pager.setPage(page);
        pager.setSize(pageSize);
        pager.setData(list);
        pager.setTotal(total);

        return pager;
    }

    @Override
    public Pager<LocationHistoryVO> getLocationHistory(Long cattleId, Integer page, Integer pageSize) {
        int offset = (page - 1) * pageSize;
        List<LocationHistoryVO> list = cattleMapper.selectLocationHistory(cattleId, offset, pageSize);
        int total = cattleMapper.countLocationHistory(cattleId);

        Pager<LocationHistoryVO> pager = new Pager<>();
        pager.setPage(page);
        pager.setSize(pageSize);
        pager.setData(list);
        pager.setTotal(total);

        return pager;
    }
    
    @Override
    public List<SicknessHistoryVO> getSicknessHistoryNoPaging(Long cattleId) {
        return cattleMapper.selectSicknessHistoryNoPaging(cattleId);
    }

    @Override
    public List<SicknessHistoryVO> getAllSicknessHistory() {
        return cattleMapper.selectAllSicknessHistoryNoPaging();
    }

    @Override
    public CattleDetailVO getCattleDetail(Long cattleId) {
        CattleDetailVO cattleDetail = cattleMapper.selectCattleDetail(cattleId);
        return cattleDetail;
    }
    
    @Override
    public Cattle addCattle(Cattle cattle) {
        // 插入牛只数据
        cattleMapper.insert(cattle);
        return cattle;
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteCattleAndAddDeathRecord(DeathCattleVO deathCattleVO) {
        // 1. 获取要删除的牛只信息
        Cattle cattle = cattleMapper.selectById(deathCattleVO.getCattleId());
        if (cattle == null) {
            return false;
        }
        
        // 2. 创建死亡/出栏记录
        DeathCattle deathCattle = new DeathCattle();
        deathCattle.setCattleId(cattle.getId().intValue());
        deathCattle.setAdditionReason(cattle.getAdditionReason());
        deathCattle.setCost(cattle.getCost());
        deathCattle.setDeletionReason(deathCattleVO.getDeletionReason());
        deathCattle.setRevenue(deathCattleVO.getRevenue());
        
        // 计算利润 = 收入 - 成本
        BigDecimal profit = deathCattleVO.getRevenue().subtract(cattle.getCost());
        deathCattle.setProfit(profit);
        
        // 设置删除日期为当前时间
        deathCattle.setDeletedAt(new Date());
        
        // 3. 插入死亡/出栏记录
        cattleMapper.insertDeathCattle(deathCattle);
        
        // 4. 标记牛只为已删除状态，而不是物理删除
        cattle.setIsDeleted(1); // 1表示已删除
        // 移除设置 deletedAt 的代码
        cattleMapper.updateCattleStatus(cattle);
        
        return true;
    }
    
    @Override
    public List<CattleManagementVO> getAllCattleList() {
        // 获取所有牛只（包括已出栏的）
        return cattleMapper.selectAllCattleWithDeathInfo();
    }
}
